package com.example.login.server;

import com.example.login.client.GreetingService;
import com.example.login.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import javax.servlet.http.HttpSession;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {



	public String getUserName()  
	{  
	    HttpSession session = getThreadLocalRequest().getSession(true);  
	    if (session.getAttribute("userName") == null)  
	    {  
	    	session.setAttribute("userName", "GOYAL");  
	        return   "SET-GOYAL";
	    }  
	    else   
	    {  
	    	return (String) session.getAttribute("userName");
	    } 
	    //return "PROBLEM";
	}	
	
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}
	    
	
		return getUserName();
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
}
